package org.dtp.examples.apollo.controller;

import org.dtp.DynamicThreadPoolExecutor;
import org.dtp.DynamicThreadPoolManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * @ClassName DynamicThreadController
 * @Description 测试接口
 * @Author wangyiqi
 * @Date 2021-08-13-19:56
 **/
@RestController
@RequestMapping("/dynamicThread")
public class DynamicThreadController {

    @Resource
    private DynamicThreadPoolManager dynamicThreadPoolManager;

    @GetMapping("/queryAll")
    public Map<String, Map<String, String>> getCurrentAllThreadInfo() {
        Map<String, Map<String, String>> infos = new HashMap<>(16);
        dynamicThreadPoolManager.getThreadPoolName().forEach(p -> {
            DynamicThreadPoolExecutor threadPoolExecutor = dynamicThreadPoolManager.getThreadPoolExecutor(p);
            Map<String, String> info = new HashMap<>(16);
            info.put("corePoolSize", String.valueOf(threadPoolExecutor.getCorePoolSize()));
            info.put("maximumPoolSize", String.valueOf(threadPoolExecutor.getMaximumPoolSize()));
            info.put("activeCount", String.valueOf(threadPoolExecutor.getActiveCount()));
            info.put("completedTaskCount", String.valueOf(threadPoolExecutor.getCompletedTaskCount()));
            info.put("largestPoolSize", String.valueOf(threadPoolExecutor.getLargestPoolSize()));
            info.put("taskCount", String.valueOf(threadPoolExecutor.getTaskCount()));
            info.put("queue", threadPoolExecutor.getQueue().toString());
            info.put("rejectedExecutionHandler", threadPoolExecutor.getRejectedExecutionHandler().toString());
            infos.put(p, info);
        });
        return infos;
    }

    @GetMapping("/queryInfo/{threadName}")
    public Map<String, String> getCurrentThreadInfo(@PathVariable("threadName") String threadName) {
        DynamicThreadPoolExecutor threadPoolExecutor = dynamicThreadPoolManager.getThreadPoolExecutor(threadName);
        Map<String, String> infos = new HashMap<>(16);

        infos.put("corePoolSize", String.valueOf(threadPoolExecutor.getCorePoolSize()));
        infos.put("maximumPoolSize", String.valueOf(threadPoolExecutor.getMaximumPoolSize()));
        infos.put("activeCount", String.valueOf(threadPoolExecutor.getActiveCount()));
        infos.put("completedTaskCount", String.valueOf(threadPoolExecutor.getCompletedTaskCount()));
        infos.put("largestPoolSize", String.valueOf(threadPoolExecutor.getLargestPoolSize()));
        infos.put("taskCount", String.valueOf(threadPoolExecutor.getTaskCount()));
        infos.put("waitTaskCount", String.valueOf(threadPoolExecutor.getQueue().size()));
        return infos;
    }

    @GetMapping("/pushTask/{threadName}")
    public Map<String, String> pushTask(@PathVariable("threadName") String threadName) throws InterruptedException {
        DynamicThreadPoolExecutor threadPoolExecutor = dynamicThreadPoolManager.getThreadPoolExecutor(threadName);
        Map<String, String> infos = new HashMap<>(16);
        CountDownLatch countDownLatch = new CountDownLatch(1);
        threadPoolExecutor.execute(() -> {
            try {
                long sleepTime = (long) (Math.random() * 500 + 500);
                Thread.sleep(sleepTime);
                infos.put(threadName, "sleep：" + sleepTime);
                countDownLatch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        countDownLatch.await();
        return infos;
    }
}
